package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @Name : IProductServiceImpl.java
 * @Time : 2021/8/25 18:12
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */

@Service
@Slf4j
public class IProductServiceImpl implements IProductService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    @Autowired
    private ProductConverter productConverter;

    @Autowired
    private PanelMapper panelMapper;

    @Autowired
    private PanelContentMapper panelContentMapper;

    @Autowired
    private ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Long id = request.getId();
            Item item = itemMapper.selectByPrimaryKey(id);
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
            List<String> productImageSmall = new ArrayList<>();
            productImageSmall.addAll(Arrays.asList(item.getImages()));
            ProductDetailDto productDetailDto = new ProductDetailDto(id, item.getPrice(), item.getTitle(), item.getSellPoint(), (long) item.getLimitNum(), item.getImageBig(), itemDesc.getItemDesc(), productImageSmall);

            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IProductServiceImpl.getProductDetail occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();

        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            Example example = new Example(Item.class);
            String sort = request.getSort();
            if (sort != null && !"".equals(sort)) {
                if (sort.equals("1")) sort = "asc";
                if (sort.equals("-1")) sort = "desc";
                example.setOrderByClause("price " + sort);
            }

            Example.Criteria criteria = example.createCriteria();
            String priceGt = request.getPriceGt();
            if (priceGt != null && !"".equals(priceGt)) {
                BigDecimal bigDecimal1 = new BigDecimal(priceGt);
                criteria.andGreaterThan("price", bigDecimal1);
            }
            String priceLte = request.getPriceLte();
            if (priceLte != null && !"".equals(priceLte)) {
                BigDecimal bigDecimal2 = new BigDecimal(priceLte);
                criteria.andLessThan("price", bigDecimal2);
            }

            if(request.getKey() != null && !"".equals(request.getKey())){
                criteria.andLike("title","%" + request.getKey() + "%");
            }

            List<Item> items = itemMapper.selectByExample(example);
            List<ProductDto> productDtos = productConverter.item2Dto(items);

            PageInfo<Item> itemPageInfo = new PageInfo<>(items);
            long total = itemPageInfo.getTotal();

            response.setData(productDtos);
            response.setTotal(total);

            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IProductCateServiceImpl.getAllProduct occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
//            List<Panel> panels = panelMapper.selectPanelContent(GlobalConstants.RECOMMEND_PANEL_ID);
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                panel.setPanelContentItems(panelContentItems);
            }

            List<PanelDto> panelDtos = contentConverter.panel2DTO(panels);

            response.setPanelContentItemDtos(new HashSet<>(panelDtos));
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IHomeServiceImpl.homepage occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
